<?php


//define('DEBUGPNG', 1);
//define('DEBUGCSS', 1);

Yii::import('application.extensions.html2pdf.PdfGenerator');
Yii::import('application.extensions.html2pdf.CHtml2PdfContext');
Yii::import('application.extensions.html2pdf.IHtml2PdfFieldParser');

class DefaultController extends CController {

	public function init()
	{
		parent::init();
	}

	public function actionIndex() {
		$this->render('index');
	}

	public function actionRender() {
		global $report_path_in;
		global $report_path_out;
		$id = $_GET['id'];
		$edited_report = EditedReport::model()->findByPk($id);
		$project = $edited_report->project;
		$filename = $report_path_out . DIRECTORY_SEPARATOR . $edited_report->filename . ".pdf";

		if (!is_dir($report_path_out))
		mkdir ($report_path_out, '0777', true);

		chdir($report_path_in );
		
		Yii::log('Report file is '.$filename.'.', CLogger::LEVEL_INFO, "ReportController[#$id]");
		if (!file_exists($filename) || isset($_GET['force']) || isset($_GET['vuehtml'])) {
			ob_start();
			include(str_replace(DIRECTORY_SEPARATOR, "/", "." . DIRECTORY_SEPARATOR . $edited_report->report->template));
			$content = ob_get_clean();
				
			$context = new CHtml2PdfContext();
			
			if (setlocale (LC_ALL, 'fra') === false) {
				die('loacle not found');
			}
			
			$context->put('project', $edited_report->project);
			$context->put('owner', $edited_report->project->owner);
			$context->put('adviser', Yii::app()->user->dbuser);
			$context->put('meeting', $edited_report->meeting);
			$ownerData = array();

			$specificData = Yii::app()->parameter->getParametersArray('OWNER_SPEC')->toAssociativeArray();
			foreach ($edited_report->project->owner->specific_data as $id => $specificDatum)
			{
				$ownerData[$specificDatum->field_name] = (object)array(
					'label' => $specificData[$specificDatum->field_name]['FLDLBL'],
					'value' => $specificDatum->field_value
				);
			}
			$context->put('ownerData', (object) $ownerData);
			
			foreach ($edited_report->parameters as $parameter) {
				$context->put($parameter->name, $parameter->attributes);
			}
			
			// init HTML2PDF
			$html2pdf = new PdfGenerator('P', 'A4', 'fr', true, 'UTF-8', array(0, 0, 0, 0));

			// display the full page
			$html2pdf->html2pdf->pdf->SetDisplayMode('fullpage');

			// convert
			$html2pdf->writeHTML(str_replace(DIRECTORY_SEPARATOR, "/", $report_path_in . "/" . DIRECTORY_SEPARATOR . $edited_report->report->template), $context, isset($_GET['vuehtml']));
			
			// send the PDF
			$html2pdf->Output($filename, "F");
		}
		
		if (!isset($_GET['noattach']))
			header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
		else
			header('Content-Type:application/pdf');
		readfile($filename);
	}

	protected function findPreviousEditions($edited_report) {
		$criteria = new CDbCriteria;
		$criteria->compare('id', "<>" . $edited_report->id);
		$criteria->compare('report_id', $edited_report->report_id);
		$criteria->compare('project_id', $edited_report->project_id);
		$criteria->compare('edition_date', "<" . $edited_report->edition_date);
		$criteria->order = 'edition_date desc';

		$data = new CActiveDataProvider('EditedReport', array (
			'criteria' => $criteria,
			
		));

		return $data->getData();
	}

}